package ssn.common.util;

import java.security.MessageDigest;

/**
 * @Class Name : MD5Hash.java
 * @Description : MD5 암호화
 * @Modification @
 * @ 생성일 : 2011.10.10
 * @ 수정일 :
 * @ 수정자 :
 * @ 수정내용 :
 * @ 최초생성 :
 *
 */
public class MD5Hash {

	public String getHash(String inputData) {
		byte[] data_hash = null;
		String data_String = "fail";
		String returnStr = "";
		try {

			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(inputData.getBytes());
			data_hash = md.digest();

		} catch (Exception e) {
			data_hash = data_String.getBytes();
		} finally {
			returnStr = transString(data_hash);
		}
		return returnStr;
	}

	public String transString(byte[] input) {

		if (input == null) {
			return "";
		}
        String dataString = "";
        String hexdata = "";

        int[] ninput = new int[input.length];

        for(int i=0 ; i < input.length ; i++) {
        	ninput[i] = ((int) input[i])+128;
        }

        for(int j=0; j < ninput.length ; j++) {
        	if(ninput[j] <= 0x0f) {
        	     hexdata = "0"+Integer.toHexString(ninput[j]);
        	} else {
        	     hexdata = Integer.toHexString(ninput[j]);
        	}
            dataString += hexdata;
        }

        return dataString;
	}
}
